package com.lanswon.controller.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.entity.BottomNavigation;
import com.lanswon.service.BottomNavService;

@Controller
@RequestMapping("admin/bottomnav")
public class AdminBottomNavController {
	
	@Autowired
	private BottomNavService bottomNavService;
	
	@RequestMapping("index")
	public String index(HttpServletRequest request,HttpServletResponse response,ModelMap modelMap) {
		
		List<BottomNavigation> bnList =  bottomNavService.findBottomNavList();
		if(bnList!=null&&bnList.size()>0) {
			modelMap.addAttribute("bottomNav", bnList.get(0));
		}
		return "admin/bottomnav/index";
	}
	
	@RequestMapping("addAndUpdateBottomNav")
	@ResponseBody
	public Map<String, Object> addAndUpdateBottomNav(HttpServletRequest request,HttpServletResponse response,BottomNavigation bottomNav){
		Map<String, Object> viewMap = new HashMap<>();
		
		try {
			bottomNav.setUpdateTime(new Date());
			if(bottomNav.getId()!=null) {
				bottomNavService.updateBottomNav(bottomNav);
			}else {
				bottomNavService.addBottomNav(bottomNav);
			}
			viewMap.put("code", 1);
			viewMap.put("msg", "保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			viewMap.put("code", 2);
			viewMap.put("msg", "操作有误");
		}
		return viewMap;
	}
}
